use dta/DataSet_v28.dta, clear

gen marginal=.
replace marginal=1 if margin!=. & abs(margin)<0.05

gen electedXyear=elected*year
replace electedXyear=. if electedXyear==0
egen first_elected=min(electedXyear), by(pid)

gen marginalXyear=marginal*year
egen first_marginal=min(marginalXyear), by(pid)

keep if year>1950
sort year pid

********************************************************************************************
*********************** MAKE SEAT RANK SECURITY FOR EACH YEAR ******************************
********************************************************************************************
preserve
gen regular_seat=elected
egen test=sum(elected), by(year)
assert test==parliament if year<2017 
collapse (sum) elected, by(year party districtid)
rename elected party_seatsLAG
drop if year==2017
replace year=year+4 /* for merging */
keep year districtid party party_seatsLAG
sort year districtid party
save dta/temp.dta, replace
restore

sort year districtid party
merge year districtid party using dta/temp.dta
drop if _merge==2
drop _merge
replace party_seatsLAG=0 if party_seatsLAG==.
gen rankstar=party_seatsLAG-rank
*********************************************************************************
******************************FIXING DUPLICATES *********************************
*********************************************************************************

bysort pid year: egen min_rank=min(rank)
keep if rank==min_rank  /* keeping only the highest ranked entry for each candidate */
drop if party=="v" & year==2013 & district=="aust-agder" /* Venstre ran with identical lists in Vest-Agder and Aust-Agder in 2013. Venstre were 8.5pp away from winning a regular seat in Vest-Agder, and 13.1pp away from winning a regular seat in Aust-Agder, so we keep Vest-Agder */
drop if party=="v" & year==2017 & district=="aust-agder"
gen temp=1
egen count=sum(temp), by(pid year)
drop if count==2 /* Venstre ran with identical lists in Vest-Agder and Aust-Agder in 2013. Venstre were 8.5pp away from winning a regular seat in Vest-Agder, and 13.1pp away from winning a regular seat in Aust-Agder. Drop both */

drop if candidatename_ed=="Anders Lange" & district=="vest-agder" /* Anders Lange was ranked first in both Oslo and Vest-Agder, but performed best in Oslo */
*drop if candidatename_ed=="Svend Haakon Jacobsen" & district=="rogaland" /* runs in both districts as hopeless candidate but same rank, arbitrary exclude one */
*drop if candidatename_ed=="Kristin Dalehamn" & district=="rogaland" /* runs in both districts as hopeless candidate but same rank, arbitrary exclude one */
*drop if candidatename_ed=="Steinar Bastesen" & district=="nord-trøndelag" /* runs in two districts in 2001, and wins in Nordland */

********************************************************************************************
************************CREATE VARIABLES AND MAKE SAMPLE************************************
********************************************************************************************
gen hopeless=0
replace hopeless=1 if rankstar<0

gen hot=0
replace hot=1 if rankstar==0

gen safe=0
replace safe=1 if rankstar>0

gen run=1

keep if inlist(party, "sv", "dna", "sp", "v", "krf", "h", "frp", "h_fv")

gen zz=1
egen sum_zz=sum(zz), by(pid year)
drop if sum_zz!=1 /* candidates running in multiple districts */

xtset pid year
sort pid year
foreach var in hopeless hot safe elected run {
gen `var'_next=`var'[_n+1]
replace `var'_next=0 if pid!=pid[_n+1]    /* different candidate */
replace `var'_next=0 if year!=year[_n+1]-4   /* not the next election */
replace `var'_next=0 if pid==pid[_n+1] & party!=party[_n+1] /* different party */
}

rename run_next cand_next1
rename elected_next seat_next1
gen winnable_next=safe_next+hot_next

********************************************************************************************
********************************************************************************************
********************************************************************************************
drop if year==2021 /* don't have future position */
li year party candidatename_ed deputy elected margin first_elected first_marginal electedXyear marginalXyear if pid==36832
*keep if abs(margin)<0.05
li year party candidatename_ed deputy elected margin first_elected first_marginal electedXyear marginalXyear if pid==36832
sum year margin /* 26,868 obs */
drop if first_elected<year & first_elected!=0 /* candidates previously elected */
drop if first_marginal<year & first_marginal!=0 /* candidates previously close to elected */
*drop if first_year_cabinet<year /* previously cabinet member */
sum margin if abs(margin)<0.05 /* 1066 obs */
sum margin

 ************************************************************************************
 ****      BASELINE ANALYSIS     ****************************************************
 ************************************************************************************
gen margin100=margin*100
 
	foreach var in winnable_next {
	rdplot `var' margin100 if abs(margin100)<5 & year>1950 & female==1 , p(1) nbins(5 5) graph_options(xtitle(Win margin (t)) ytitle(Fraction) ///
	title(Women) graphregion(fcolor(white) lcolor(white)) scheme(s2mono)  legend(off) yscale(range(0 1)) ylabel(0[0.2]1, angle(0)) xlabel(-5(2)5) xsize(7) ysize(7) scale(1.5) )
	graph save figures/gph/f1.gph, replace
	} 

 	foreach var in winnable_next {
	rdplot `var' margin100 if abs(margin100)<5 & year>1950 & year<1980 & female==0 , p(1) nbins(5 5) graph_options(xtitle(Win margin (t)) ytitle(Fraction) ///
	title(Men) graphregion(fcolor(white) lcolor(white)) scheme(s2mono)  legend(off) yscale(range(0 1)) ylabel(0[0.2]1, angle(0)) xlabel(-5(2)5)  xsize(7) ysize(7) scale(1.5) )
	graph save figures/gph/m1.gph, replace
	} 

 
gen seat=0
replace seat=1 if margin>0 & margin!=.
gen seatXmargin=seat*margin

foreach var in winnable_next {
estimates clear
reg `var' seat margin seatXmargin if abs(margin)<0.05 & female==1 & year>1950, cluster(pid)
eststo
reg `var' seat margin seatXmargin if abs(margin)<0.05 & female==0 & year>1950, cluster(pid)
eststo  
}

***************************************************************************
********************** WOMEN VARYING BW ***********************************
***************************************************************************
preserve
postfile dataset str10 (depvar) BW pointest lower upper using estimates/bw.dta, replace

rdrobust winnable_next margin if female==1 & year>1950, p(1) kernel(uniform)
rdrobust winnable_next margin if female==0 & year>1950, p(1) kernel(uniform)
rdrobust winnable_next margin if female==1 & year>1950, p(1) kernel(uniform) h(0.05)
rdrobust winnable_next margin if female==0 & year>1950, p(1) kernel(uniform) h(0.05)


foreach depvar in winnable_next{
	forvalues BW=0.01(0.005)0.16 {
	reg `depvar' seat margin seatXmargin if abs(margin)<`BW' & female==1 & year>1950, vce(cluster pid)
	post dataset ("`depvar'") (`BW') (_b[seat]) (_b[seat]-1.96*_se[seat]) (_b[seat]+1.96*_se[seat])
	}
}
postclose dataset

use estimates/bw.dta, clear
label var BW "Bandwidth"

twoway (line lower BW , lwidth(thin) lpattern(dash)) (line upper BW , lwidth(thin) lpattern(dash) lcolor(gray)) (line pointest BW, lpattern(solid) lwidth(thick) lcolor(gray)), ///
ytitle(RD estimate) yscale(range(-0.8 0.8)) ylabel(-0.8(0.4)0.8, angle(0)) xline(0.023 0.046, lpattern(shortdash) lcolor(gray*0.75)) scale(1.5) ///
legend(off) title(Women)  plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) yline(-0.8 -0.4 0 0.4 0.8, lcolor(gray*0.1))
graph save figures/gph/women1.gph, replace
restore

***************************************************************************
********************* MEN VARYING BW **************************************
***************************************************************************
preserve
postfile dataset str10 (depvar) BW pointest lower upper using estimates/bw.dta, replace

foreach depvar in winnable_next{
	forvalues BW=0.01(0.005)0.16 {
	reg `depvar' seat margin seatXmargin if abs(margin)<`BW' & female==0 & year>1950, vce(cluster pid)
	post dataset ("`depvar'") (`BW') (_b[seat]) (_b[seat]-1.96*_se[seat]) (_b[seat]+1.96*_se[seat])
	}
}
postclose dataset

use estimates/bw.dta, clear
label var BW "Bandwidth"

twoway (line lower BW , lwidth(thin) lpattern(dash)) (line upper BW , lwidth(thin) lpattern(dash) lcolor(gray)) (line pointest BW, lpattern(solid) lwidth(thick) lcolor(gray)), ///
ytitle(RD estimate) yscale(range(-0.8 0.8)) ylabel(-0.8(0.4)0.8, angle(0)) xline(0.046 0.092, lpattern(shortdash) lcolor(gray*0.75)) scale(1.5) ///
legend(off) title(Men)  plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) yline(-0.8 -0.4 0 0.4 0.8, lcolor(gray*0.1))
graph save figures/gph/Men1.gph, replace
restore

cd figures/gph
graph combine f1.gph m1.gph Women1.gph Men1.gph , plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono)
graph export ../FigureA9.pdf, replace
cd ../../
